﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO_DTO_WS.DAO;
using DAO_DTO_WS.DTO;

namespace LayoutTemplate
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"].ToString() == "")
                Response.Redirect("TrangLoi.aspx?Error=2");
            if (!IsPostBack)
            {
             
                long tongtien = 0;
                int tongsoluong = 0;
                int id = int.Parse(Request.QueryString["id"].ToString());
                List<DonHangDTO> dsdonhang = DonHangDAO.layDonHangTheoMaSanPham(id);
                List<ChiTietKhuyenMaiDTO> dsChiTiet = new List<ChiTietKhuyenMaiDTO>();
                for (int i = 0; i < dsdonhang.Count; ++i)
                {
                    ChiTietKhuyenMaiDTO chitiet = new ChiTietKhuyenMaiDTO();
                    SanPhamDTO sanpham = SanPhamDAO.laySPTheoMa(dsdonhang[i].MaSanPham);
                    ThanhVienDTO thanhvien = ThanhVienDAO.LayThanhVienTheoMa(dsdonhang[i].MaThanhVien);
                    chitiet.DonGia = sanpham.GiaTietKiem;
                    switch (dsdonhang[i].MaLoaiThanhToan)
                    {
                        case 1:
                            chitiet.LoaiThanhToan = "NganLuong";
                            break;
                        case 2:
                            chitiet.LoaiThanhToan = "PayPal";
                            break;
                    }
                    chitiet.NgayMua = dsdonhang[i].NgayMua;
                    chitiet.SoLuong = dsdonhang[i].SoLuong;
                    chitiet.TenNguoiMua = thanhvien.TenThanhVien;
                    chitiet.TenSanPham = sanpham.TieuDe;
                    switch (dsdonhang[i].TinhTrang)
                    {
                        case 0:
                        case 2:
                            chitiet.TinhTrang = "Đã Thanh Toán";
                            break;
                        case 1:
                            chitiet.TinhTrang = "Chưa Thanh Toán";
                            break;
                    }
                    chitiet.TongTien = dsdonhang[i].TongTien;
                    dsChiTiet.Add(chitiet);
                    tongsoluong += chitiet.SoLuong;
                    tongtien += chitiet.TongTien;
                }
                SanPhamDTO sp = SanPhamDAO.laySPTheoMa(id);
                lbGiaBanDau.Text=sp.GiaGoc.ToString();
                lbGiaBanGiam.Text = sp.GiaTietKiem.ToString(); ;
                lbSlg.Text=sp.SoLuong.ToString();
                lbSoLuongToiDa.Text=sp.SoLuongToiDa.ToString();
                lbSoLuongToiThieu.Text=sp.SoLuongToiThieu.ToString();
                lbSoLuotXem.Text=sp.SoLanXem.ToString();
                lbTenSanPham.Text= sp.TieuDe;
                lbThoiGianBatDau.Text=sp.ThoiGianBatDau.ToShortDateString();
                lbThoiGianKetThuc.Text=sp.ThoiGianKetThuc.ToShortDateString();
                lbThongTinSanPham.Text=sp.ThongTin;
     
                lbTongSoLuong.Text = tongsoluong.ToString();
                lbTongTien.Text = tongtien.ToString();
                GridView1.DataSource = dsChiTiet;
                GridView1.DataBind();
            }
        }

        protected void btnTroLai_Click(object sender, EventArgs e)
        {
            Response.Redirect("DoanhNghiep_DanhSachCacTinKhuyenMai.aspx");
        }
    }
}
